package com.huluobo.a2202mvvm_im_douyindemo.animDemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.AnticipateInterpolator;
import android.view.animation.BounceInterpolator;
import android.view.animation.CycleInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.OvershootInterpolator;

import com.huluobo.a2202mvvm_im_douyindemo.R;
import com.huluobo.a2202mvvm_im_douyindemo.databinding.ActivityAnimBinding;
import com.huluobo.a2202mvvm_im_douyindemo.mvvmBase.BaseActivity;

public class AnimActivity extends BaseActivity<ActivityAnimBinding> {

    @Override
    protected void initData() {

    }

    @Override
    protected void initView() {
        //平移
        Animation animationTran = AnimationUtils.loadAnimation(this, R.anim.anim_translate);
        dataBinding.tran.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationTran);
        });

        //旋转
        Animation animationRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
        dataBinding.rotation.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationRotate);
        });

        //缩放
        Animation animationScale = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
        dataBinding.scale.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationScale);
        });

        //渐变动画(透明度)
        Animation animationAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
        dataBinding.alpha.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationAlpha);
        });

        //动画集合
        Animation animationSet = AnimationUtils.loadAnimation(this, R.anim.anim_set);
        dataBinding.set.setOnClickListener(v -> {
            dataBinding.imageView.startAnimation(animationSet);
        });

        //帧动画
        AnimationDrawable animationDrawable = (AnimationDrawable) dataBinding.progressIv.getDrawable();
        dataBinding.progressBtn.setOnClickListener(v -> {
            animationDrawable.start();
        });

        //属性动画
        dataBinding.valueAnimatorBtn.setOnClickListener(v -> {
            valueAnim();
        });

        dataBinding.objectAnimatorBtn.setOnClickListener(v -> {
            objectAnim();
        });

        dataBinding.interpolator.setOnClickListener(v -> {
            interpolator();
        });
    }

    //给动画设置插值器,默认的插值器是加速减速插值器
    public void interpolator() {
        ObjectAnimator animator = ObjectAnimator.ofFloat(dataBinding.imageView, "translationY", -400, 1000);

        animator.setInterpolator(new LinearInterpolator());//线性插值器,可以进行匀速直线运动
//        animator.setInterpolator(new AccelerateInterpolator());//加速插值器
//        animator.setInterpolator(new DecelerateInterpolator());//减速插值器
//        animator.setInterpolator(new AccelerateDecelerateInterpolator());//默认的,加速减速插值器
//        animator.setInterpolator(new AnticipateInterpolator());//反向加速插值器
//        animator.setInterpolator(new OvershootInterpolator());//超越插值器
//        animator.setInterpolator(new BounceInterpolator());//探求插值器
//        animator.setInterpolator(new CycleInterpolator());//循环插值器也就是 超越和反向的组合

        animator.setDuration(5000);
        animator.setRepeatMode(ValueAnimator.RESTART);
        animator.setRepeatCount(-1);
        animator.start();
    }

    /**
     * 属性动画 对象的属性动画
     */
    private void objectAnim() {
        //透明度 Alpha
        //TranslationX x轴平移
        //TranslationY y轴平移
        //Rotation 旋转
        //ScaleX x轴的缩放
        //ScaleY y轴的缩放
//        dataBinding.imageView.setScaleX();
//        ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(dataBinding.imageView, "ScaleX", 1f, 2f);
//        objectAnimator.setDuration(5000);
//        objectAnimator.start();

        ObjectAnimator rotate = ObjectAnimator.ofFloat(dataBinding.imageView, "Rotation", 0, 360);
        ObjectAnimator translateX = ObjectAnimator.ofFloat(dataBinding.imageView, "TranslationX", -100, 100);
        ObjectAnimator translateY = ObjectAnimator.ofFloat(dataBinding.imageView, "TranslationY", -100, 100);
        ObjectAnimator scaleX = ObjectAnimator.ofFloat(dataBinding.imageView, "ScaleX", 1, 2);
        ObjectAnimator scaleY = ObjectAnimator.ofFloat(dataBinding.imageView, "ScaleY", 1, 2);
        ObjectAnimator alpha = ObjectAnimator.ofFloat(dataBinding.imageView, "Alpha", 1, 0, 1);

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(rotate)//3 要执行的效果,必须要有个play
                .with(alpha)//3 与play同时执行
                .with(scaleX)//3 与play同时执行
                .after(scaleY)//1 执行完再执行play
                .after(2000)//2 将play延时2秒后执行,也就是先执行2秒,再执行play
                .before(translateX)//4 执行完play,再执行
                .before(translateY);//4 执行完play,再执行

        animatorSet.setDuration(10000);
        animatorSet.start();

    }

    /**
     * 属性动画的方法 valueAnimator
     */
    public void valueAnim() {
        ValueAnimator valueAnimator = ValueAnimator.ofInt(0, 360);//设置起始点和终点
        valueAnimator.setDuration(5000);//设置时长
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.setRepeatCount(ValueAnimator.INFINITE);//重复模式,infinite无限循环
        valueAnimator.setRepeatMode(ValueAnimator.RESTART);
        valueAnimator.addUpdateListener(animation -> {
            //拿到属性动画中的值
            int currentValue = (int) animation.getAnimatedValue();
            Log.i("TagA", "属性动画监听器中的值:" + currentValue);
            //通过值给imageView设置透明度
            dataBinding.imageView.setRotation(currentValue);
        });
        valueAnimator.start();
    }


    @Override
    protected int getLayoutId() {
        return R.layout.activity_anim;
    }
}